#include "../Tools/GLHeaders.hpp"
#include "ModelPelvis.hpp"
#include <cmath>

ModelPelvis::ModelPelvis(void){
	activate = false;	
  list_id = glGenLists(1); // create one DisplayList
  glNewList(list_id, GL_COMPILE);
  GLUquadricObj *quadratic = gluNewQuadric();
  gluQuadricNormals(quadratic, GLU_SMOOTH);

  GLfloat pt00[] = { 0.0f,-4.0f, 1.0f};
  GLfloat pt01[] = { 2.5f,-3.5f, 1.0f};
  GLfloat pt02[] = { 2.5f,-2.5f, 1.0f};
  GLfloat pt03[] = { 1.5f,-2.0f, 1.0f};
  GLfloat pt04[] = { 1.0f,-1.0f, 1.0f};
  GLfloat pt05[] = {-1.0f,-1.0f, 1.0f};
  GLfloat pt06[] = {-1.5f,-2.0f, 1.0f};
  GLfloat pt07[] = {-2.5f,-2.5f, 1.0f};
  GLfloat pt08[] = {-2.5f,-3.5f, 1.0f};

  GLfloat pt10[] = { 0.0f,-4.0f,-1.0f};
  GLfloat pt11[] = { 2.5f,-3.5f,-1.0f};
  GLfloat pt12[] = { 2.5f,-2.5f,-1.0f};
  GLfloat pt13[] = { 1.5f,-2.0f,-1.0f};
  GLfloat pt14[] = { 1.0f,-1.0f,-1.0f};
  GLfloat pt15[] = {-1.0f,-1.0f,-1.0f};
  GLfloat pt16[] = {-1.5f,-2.0f,-1.0f};
  GLfloat pt17[] = {-2.5f,-2.5f,-1.0f};
  GLfloat pt18[] = {-2.5f,-3.5f,-1.0f};

  GLfloat pt20[] = {-3.25f,-3.25f, 0.25f};
  GLfloat pt21[] = {-2.50f,-3.25f, 0.25f};
  GLfloat pt22[] = {-2.50f,-2.75f, 0.25f};
  GLfloat pt23[] = {-3.25f,-2.75f, 0.25f};

  GLfloat pt30[] = {-3.25f,-3.25f,-0.25f};
  GLfloat pt31[] = {-2.50f,-3.25f,-0.25f};
  GLfloat pt32[] = {-2.50f,-2.75f,-0.25f};
  GLfloat pt33[] = {-3.25f,-2.75f,-0.25f};

  GLfloat pt40[] = { 2.50f,-3.25f, 0.25f};
  GLfloat pt41[] = { 3.25f,-3.25f, 0.25f};
  GLfloat pt42[] = { 3.25f,-2.75f, 0.25f};
  GLfloat pt43[] = { 2.50f,-2.75f, 0.25f};

  GLfloat pt50[] = { 2.50f,-3.25f,-0.25f};
  GLfloat pt51[] = { 3.25f,-3.25f,-0.25f};
  GLfloat pt52[] = { 3.25f,-2.75f,-0.25f};
  GLfloat pt53[] = { 2.50f,-2.75f,-0.25f};
  //Pelvis
  glBegin(GL_TRIANGLES);
  glNormal3f( 0.0f, 0.0f, 1.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt00); // Front
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt01);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt02);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt00);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt02);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt03);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt00);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt03);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt04);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt00);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt04);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt05);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt00);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt05);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt06);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt00);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt06);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt07);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt00);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt07);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt08);

  glNormal3f( 1.0f, 0.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt01); // Right
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt11);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt12);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt12);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt02);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt01);

  glNormal3f(-1.0f, 0.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt17); // Left
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt18);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt08);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt08);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt07);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt17);

  glNormal3f( 0.0f, 0.0f,-1.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt12); // Back
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt11);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt10);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt13);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt12);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt10);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt14);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt13);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt10);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt15);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt14);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt10);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt16);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt15);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt10);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt17);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt16);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt10);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt18);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt17);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt10);

  glNormal3f( cosf(-M_PI/6.0f), sinf(-M_PI/6.0f), 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt00); // Right Bottom
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt10);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt11);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt11);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt01);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt00);

  glNormal3f( cosf(-5.0f*M_PI/6.0f), sinf(-5.0f*M_PI/6.0f), 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt10); // Left Bottom
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt00);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt08);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt08);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt18);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt10);

   glNormal3f( cosf(M_PI/3.0f), sinf(M_PI/3.0f), 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt03); // Right Top
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt02);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt12);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt12);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt13);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt03);

  glNormal3f( cosf(2.0f*M_PI/3.0f), sinf(2.0f*M_PI/3.0f), 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt07); // Left Top
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt06);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt16);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt16);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt17);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt07); 

  glNormal3f( 0.0f, 1.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt05); // Top
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt04);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt14);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt14);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt15);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt05);

  glNormal3f( cosf(M_PI/6.0f), sinf(M_PI/6.0f), 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt03); // Top Right
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt13);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt14);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt14);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt04);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt03); 

  glNormal3f( 1.0f, 0.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt01); // Right
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt11);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt12);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt12);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt02);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt01);

  glNormal3f( cosf(5.0f*M_PI/6.0f), sinf(5.0f*M_PI/6.0f), 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt16); // Top Left
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt06);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt05);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt05);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt15);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt16); 

  glNormal3f(-1.0f, 0.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt18); // Left
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt08);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt07);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt07);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt17);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt18);

  //Left Hip
  glNormal3f( 0.0f, 0.0f, 1.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt20); // Front
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt21);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt22);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt22);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt23);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt20);

  glNormal3f( 0.0f, 1.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt23); // Top
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt22);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt32);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt32);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt33);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt23);

  glNormal3f( 0.0f, 0.0f,-1.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt31); // Back
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt30);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt33);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt33);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt32);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt31);

  glNormal3f( 0.0f,-1.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt21); // Bottom
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt20);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt30);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt30);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt31);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt21);

  //Right Hip
  glNormal3f( 0.0f, 0.0f, 1.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt40); // Front
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt41);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt42);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt42);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt43);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt40);

  glNormal3f( 0.0f, 1.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt43); // Top
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt42);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt52);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt52);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt53);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt43);

  glNormal3f( 0.0f, 0.0f,-1.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt51); // Back
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt50);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt53);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt53);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt52);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt51);

  glNormal3f( 0.0f,-1.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt41); // Bottom
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt40);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt50);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt50);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt51);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt41);
  glEnd();

  //Left Sphere
  glTranslatef(-3.5f,-3.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);
  gluSphere(quadratic, 1.0f, 16, 16);
  glTranslatef(3.5f, 3.0f, 0.0f);
  
  //Right Sphere
  glTranslatef( 3.5f,-3.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);
  gluSphere(quadratic, 1.0f, 16, 16);
  glTranslatef(-3.5f, 3.0f, 0.0f);
  glEndList();
 }

ModelPelvis::~ModelPelvis(void){
  glDeleteLists(list_id,1);
}

void ModelPelvis::Draw(void){
	glPushAttrib(GL_COLOR_BUFFER_BIT|GL_CURRENT_BIT|GL_TEXTURE_BIT);	
	glDisable(GL_TEXTURE_2D); 
	if(activate)
		glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
	else
		glColor4f(0.0f, 1.0f, 0.0f, 1.0f);

  glCallList(list_id);
	glPopAttrib();
}

void ModelPelvis::Activate(void){
	activate = true;
}

void ModelPelvis::Desactivate(void){
	activate = false;
}
